package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SystemMenu;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.SystemMenuQueryObject;
import cn.wolfcode.service.ISystemMenuService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemMenu")
public class SystemMenuController {
    @Autowired
    private ISystemMenuService systemMenuService;


    @RequestMapping("/list")
    @RequirePermission(name = "菜单查询", expression = "systemMenu:list")
    public String list(Model model, @ModelAttribute("qo") SystemMenuQueryObject qo) {
        model.addAttribute("pageInfo", systemMenuService.query(qo));
        model.addAttribute("menuManager", systemMenuService.queryBySn("menuManager"));
        return "systemMenu/list";
    }

    @RequestMapping("/listTest")
    @RequirePermission(name = "菜单查询", expression = "systemMenu:list")
    public String listTest(Model model, @ModelAttribute("qo") SystemMenuQueryObject qo) {
        model.addAttribute("pageInfo", systemMenuService.query(qo));
        model.addAttribute("menuManager", systemMenuService.queryBySn("menuManager"));
        return "common/menuTest";
    }


    @RequestMapping("/delete")
    @RequirePermission(name = "菜单删除", expression = "systemMenu:delete")
    public String delete(Long id) {
        if (id != null) {
            systemMenuService.delete(id);
        }
        return "redirect:/systemMenu/list";
    }

    // 更新状态 启用禁用
    @RequestMapping("/updateStart")
    public String updateStart(Long id) {
        if (id != null) {
            SystemMenu systemMenu = systemMenuService.get(id);
            systemMenuService.updateStart(systemMenu, systemMenu.getStatus());
        }
        return "redirect:/systemMenu/list";
    }

    /*@RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "菜单编辑",expression = "systemMenu:saveOrUpdate")
    public String saveOrUpdate(SystemMenu systemMenu) {
        if (systemMenu.getId() != null) {
            systemMenuService.update(systemMenu);
        } else {
            systemMenuService.insert(systemMenu);
        }
        return "redirect:/systemMenu/list";
    }*/

    // 异步提交表单方式
    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "菜单编辑", expression = "systemMenu:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemMenu systemMenu) {
        if (systemMenu.getId() != null) {
            systemMenuService.update(systemMenu);
        } else {
            systemMenuService.insert(systemMenu);
        }
        return new JsonResult(true, "添加成功");
    }
}
